package com.achievo.know.application.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.achievo.know.application.FaqApplication;
import com.achievo.know.domain.Faq;
import com.achievo.know.domain.FaqRepository;
import com.achievo.know.domain.File;
import com.achievo.know.domain.FileRepository;
import com.achievo.know.domain.News;
import com.achievo.know.domain.NewsRepository;
import com.dayatang.querychannel.service.QueryChannelService;
import com.dayatang.querychannel.support.Page;

@Service
public class FaqApplicationImpl implements FaqApplication {
	
	@Autowired
	private FaqRepository faqRepository;
	@Autowired
	private QueryChannelService queryChannel;
	@Autowired
	private NewsRepository newsRepository;
	@Autowired
	private FileRepository fileRepository;
	
	

	public FileRepository getFileRepository() {
		return fileRepository;
	}


	public void setFileRepository(FileRepository fileRepository) {
		this.fileRepository = fileRepository;
	}


	public NewsRepository getNewsRepository() {
		return newsRepository;
	}


	public void setNewsRepository(NewsRepository newsRepository) {
		this.newsRepository = newsRepository;
	}


	public QueryChannelService getQueryChannel() {
		return queryChannel;
	}


	public void setQueryChannel(QueryChannelService queryChannel) {
		this.queryChannel = queryChannel;
	}


	public FaqRepository getFaqRepository() {
		return faqRepository;
	}


	public void setFaqRepository(FaqRepository faqRepository) {
		this.faqRepository = faqRepository;
	}


	@Override
	@Transactional
	public void save(Faq f) {
		faqRepository.save(f);
	}


	@Override
	public Page<Faq> getNewsFaq(Long fkId, int currentPage, int pageSize) {
		String sql="select f from Faq f where f.from=? and f.fkId=? order by f.publishTime desc";
		Object[] params=new Object[]{2,fkId};
		return queryChannel.queryPagedResultByPageNo(sql, params, currentPage, pageSize);
		//return faqRepository.listFaq(fkId.toString(),2,currentPage,pageSize);
	}
	
	@Override
	public Page<Faq> getFileFaq(Long fkId, int currentPage, int pageSize) {
		String sql="select f from Faq f where f.from=? and f.fkId=?  order by f.publishTime desc";
		Object[] params=new Object[]{1,fkId};
		return queryChannel.queryPagedResultByPageNo(sql, params, currentPage, pageSize);
		//return faqRepository.listFaq(fkId.toString(),1);
	}


	@Override
	public Faq get(Long id) {
		return faqRepository.get(id);
	}


	@Override
	public Page<Faq> queryPage(List<Long> cateIds,int currentPage, int pageSize) {
		StringBuilder hql=new StringBuilder();
		hql.append("from Faq n");
		if(cateIds!=null && cateIds.size()>0){
			StringBuilder cb=new StringBuilder();
			for(Long cateId:cateIds){
				cb.append(cateId).append(",");
			}
			if(cb.length()>0){
				cb.deleteCharAt(cb.length()-1);
				hql.append(" where n.categoryId in (").append(cb.toString()).append(")");
			}
		}
		hql.append(" order by n.publishTime desc");
		//参数
		Object[] params=new Object[]{};
		Page<Faq> page= queryChannel.queryPagedResultByPageNo(hql.toString(), params, currentPage, pageSize);
		for(Faq f:page.getResult()){
			if(f.getFrom()==2){//news
				News n=newsRepository.get(f.getFkId());
				if(n!=null){
					f.setTitle(n.getTitle());
				}
			}else{//file
				File n=fileRepository.get(f.getFkId());
				if(n!=null){
					f.setTitle(n.getFileName());
				}
			}
		}
		return page;
	}

	@Override
	@Transactional
	public void batchDelete(String[] idArr) {
		faqRepository.batchDelete(idArr);
		
	}
	
	

}
